/********************************************************************
filename : wolf_iic_AT24C64.c
discript : dreamwolf on board EEPROM(AT24C64) control driver,device 
					 address is:0b1010000x,pin connect as below:
					 IIC_SDA  -> PB7,
					 IIC_CLK  -> PB6,
					 nWC      -> GND,
					 A0,A1,A2 -> GND. 
version  : V0.0
editor   : Icy - dreamwolf
time     : 2014.3.1
statement: This file is modified by dreamwolf under LGPL,you could use 
           it free.We cann't ensure there is no error in this file,and
				   if you detect an error please contact us freely,it is so a-
					 ppreciate for your help to improve our code that could help 
					 more people to use it safty.
contact  : edreamtek@163.com
           www.edreamtek.com
********************************************************************/

#include "wolf_iic_AT24C64.h"

void IIC_Waite_AT24C64_Standby(void);

TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);


uint8_t Tx1_Buffer[] = 
"\r\nSTM32F0xx I2C Firmware Library EEPROM driver example:\r\n\
This driver will show how to write/read one byte,write/read\r\n\
muti-byte through page write and sequential Read.\r\n\
All code is collect/modified from ST webset or Internet by \r\n\
dreamwolf under LGPL.you could use it free.We cann't ensure\r\n\
there is no error in this file,and if you detect an error \r\n\
please contact us freely,it is so appreciate for your help\r\n\
to improve our code that could help more people to use it \r\n\
safty. \r\n";
uint8_t Tx2_Buffer[] = "\r\n/* Dreamwolf Ltd. */\r\n";
uint8_t Rx1_Buffer[BUFFER_SIZE1], Rx2_Buffer[BUFFER_SIZE2];
volatile TestStatus TransferStatus1 = FAILED, TransferStatus2 = FAILED;
volatile uint16_t NumDataRead = 0;
uint8_t temp;




/********************************************************************
function: wolf_iic_AT24C64_init(void)
discript: Init IIC1 port,if muti-device are used,call wolf_iic1_init()
					once.
entrance: none
return  : none
other   : none.
********************************************************************/
void wolf_iic_AT24C64_init(void)
{
	wolf_iic1_init();	
}



/********************************************************************
function: wolf_iic_AT24C64_write_byte(uint16_t addr,uint8_t dat)
discript: Write one byte in 16bit address,and wait until complete. 
entrance: addr,dat
return  : none
other   : none.
********************************************************************/
void wolf_iic_AT24C64_write_byte(uint16_t addr,uint8_t dat)
{	
	wolf_iic1_write_byte(sEE_HW_ADDRESS, ADDR_16_BITS, addr, dat);
	IIC_Waite_AT24C64_Standby();
}

/********************************************************************
function: wolf_iic_AT24C64_read_byte(uint32_t addr)
discript: Read one byte in 16bit address.
entrance: addr
return  : data
other   : none.
********************************************************************/
uint8_t wolf_iic_AT24C64_read_byte(uint32_t addr)
{
	return wolf_iic1_read_byte(sEE_HW_ADDRESS, ADDR_16_BITS, addr);
}

/********************************************************************
function: wolf_iic_AT24C64_write_bytes(uint16_t addr,uint8_t* pdat,uint16_t cnt)
discript: Write muti-byte to 16bit address
entrance: Start address,data pointer,count
return  : Status.
other   : If addr_cnt is bigger than max size of device,do nothing.
********************************************************************/
uint8_t wolf_iic_AT24C64_write_bytes(uint16_t addr,uint8_t* pdat,uint16_t cnt)
{
	uint8_t n_page,n_byte;
	uint8_t i;
	
	if(addr+cnt > sEE_MAXSIZE)
	{
		return FAIL;
	}
	else
	{
		n_page = cnt/sEE_PAGESIZE;
		n_byte = cnt%sEE_PAGESIZE;
		for(i=0; i<n_page; i++)
		{
			wolf_iic_write_bytes(sEE_HW_ADDRESS, ADDR_16_BITS, addr+i*sEE_PAGESIZE, pdat+i*sEE_PAGESIZE,sEE_PAGESIZE);	
			//IIC_Waite_AT24C64_Standby();
			delay_10ms(1);
		}
		if(n_byte>0)
		{
			wolf_iic_write_bytes(sEE_HW_ADDRESS, ADDR_16_BITS, addr+n_page*sEE_PAGESIZE, pdat+n_page*sEE_PAGESIZE,n_byte);	
			//IIC_Waite_AT24C64_Standby();
			delay_10ms(1);
		}
		
		return SUCCESS;
	}
}

/********************************************************************
function: wolf_iic_AT24C64_read_bytes(uint16_t addr,uint8_t* pdat,uint16_t cnt)
discript: Read muti-byte start with 16bit addr.
entrance: Start addr,data pointer,count
return  : Status
other   : Buffer should be big enough
********************************************************************/
uint8_t wolf_iic_AT24C64_read_bytes(uint16_t addr,uint8_t* pdat,uint16_t cnt)
{	
	if(addr+cnt > sEE_MAXSIZE)
	{
		return FAIL;
	}
	else
	{
		wolf_iic_read_bytes(sEE_HW_ADDRESS, ADDR_16_BITS, addr, pdat, cnt);
		
		return SUCCESS;
	}

}

/********************************************************************
function: IIC_Waite_AT24C64_Standby(void)
discript: Check device status,exit until it's ready.
entrance: none
return  : none
other   : none.
********************************************************************/
void IIC_Waite_AT24C64_Standby(void)
{
	
  /* Keep looping till the slave acknowledge his address or maximum number 
  of trials is reached (this number is defined by sEE_MAX_TRIALS_NUMBER define
  in stm32072b_eval_i2c_ee.h file) */
  
  /* Configure CR2 register : set Slave Address and end mode */
  I2C_TransferHandling(sEE_I2C, sEE_HW_ADDRESS, 0, I2C_AutoEnd_Mode, I2C_No_StartStop);  
  
  do
  { 
    /* Clear NACKF */
    I2C_ClearFlag(sEE_I2C, I2C_ICR_NACKCF | I2C_ICR_STOPCF);
    
    /* Generate start */
    I2C_GenerateSTART(sEE_I2C, ENABLE);
  }
  while(I2C_GetFlagStatus(sEE_I2C, I2C_ISR_NACKF) != RESET);
  
  /* Clear STOPF */
  I2C_ClearFlag(sEE_I2C, I2C_ICR_STOPCF);
  
}

/*******************************************************************************
* Function Name  : Buffercmp
* Description    : Compares two buffers.
* Input          : - pBuffer1, pBuffer2: buffers to be compared.
*                : - BufferLength: buffer's length
* Output         : None
* Return         : PASSED: pBuffer1 identical to pBuffer2
*                  FAILED: pBuffer1 differs from pBuffer2
*******************************************************************************/
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
{
  while(BufferLength--)
  {
    if(*pBuffer1 != *pBuffer2)
    {
      return FAILED;
    }

    pBuffer1++;
    pBuffer2++;
  }

  return PASSED;
}


/********************************************************************
function: wolf_iic_AT24C64_test()
discript: Test function
entrance: none
return  : none
other   : none
********************************************************************/
void wolf_iic_AT24C64_test(void)
{
	printf("Single byte read/write test:\r\n");
	//Write/read one byte.
	wolf_iic_AT24C64_write_byte(0x0000,'I');
	delay_10ms(1);
	wolf_iic_AT24C64_write_byte(0x0001,'c');
	delay_10ms(1);
	wolf_iic_AT24C64_write_byte(0x0002,'y');
	delay_10ms(1);
	
	temp = wolf_iic_AT24C64_read_byte(0x0000);
	printf("%c",temp);
	temp = wolf_iic_AT24C64_read_byte(0x0001);
	printf("%c",temp);
	temp = wolf_iic_AT24C64_read_byte(0x0002);
	printf("%c",temp);
	delay_10ms(1);
	
	printf("\r\nMulti-bytes read/write test:\r\n");

	//Write/read muti-byte.
	wolf_iic_AT24C64_write_bytes(0x0000,Tx1_Buffer,BUFFER_SIZE1);
	delay_10ms(1);
	wolf_iic_AT24C64_read_bytes(0x0000,Rx1_Buffer,BUFFER_SIZE1);
	

	printf("%s",(char*)Rx1_Buffer);
	printf("\r\n");
}


